/*
 * DISPLAY.c
 *
 * Created: 24.09.2012 14:51:56
 *  Author: mariuvi
 */
#include <stdlib.h>
 
#include "DISPLAY.h"
#include "OLED.h"
#include "definitions.h"



char DISPLAY_display[8][25];

void DISPLAY_init()
{
	OLED_init();
	DISPLAY_clear();
	DISPLAY_print();
}

void DISPLAY_print()
{
	int i,j;
	for(i = 0; i<8; i++)
	{
		for(j=0; j<25; j++)
		{
			OLED_write_char(DISPLAY_display[i][j]);
		}
		OLED_fill_rest();
	}
	OLED_reset_pointer();
}

void DISPLAY_clear()
{
	memset(DISPLAY_display, ' ', sizeof DISPLAY_display);
}



void DISPLAY_move_marker(Direction dir)
{
	int markerCol = 2;
	int markerMidRow = 4;
	int i;
	for(i=3; i<6; i++)
	{
		DISPLAY_display[i][markerCol] = ' ';
	}
	if(dir == UP)
	{
		DISPLAY_display[markerMidRow-1][markerCol] = '>';
		DISPLAY_display[markerMidRow][markerCol] = ' ';
	}
	else if(dir == DOWN)
	{
		DISPLAY_display[markerMidRow][markerCol] = ' ';
		DISPLAY_display[markerMidRow+1][markerCol] = '>';
	}
	else
	{
		DISPLAY_display[markerMidRow-1][markerCol] = ' ';
		DISPLAY_display[markerMidRow][markerCol] = '>';
		DISPLAY_display[markerMidRow+1][markerCol] = ' ';
	}
}

void DISPLAY_write_line(const char* str, int row, int col, int clear)
{
	int i = 0;
	//Adds the string
	while(str[i] != '\0')
	{
		DISPLAY_display[row][i+col] = str[i];
		i++;
	}
	if(clear)
	{
		//Clears the rest of the row
		i += col;
		for(i; i<25; i++)
		{
			DISPLAY_display[row][i] = ' '; 
		}
	}
	
}

void DISPLAY_move_menu(const char* str_above,const char* str_selected,const char* str_below)
{	
	DISPLAY_write_line(str_above, 3, 3, 1);
	DISPLAY_write_line(str_selected, 4, 3, 1);
	DISPLAY_write_line(str_below, 5, 3, 1);
}

void DISPLAY_int(int a, int row, int col)
{
	char str[25];
	sprintf(str, "%d",a);
	DISPLAY_write_line(str, row, col, 0);
}


void DISPLAY_write_line_center(const char* str, int row)
{
	int i = 0;
	
	//Get length of string
	while(str[i] != '\0')
	{
		i++;
	}
	DISPLAY_write_line(str, row, (25-i)/2, 1);
}

void DISPLAY_play_board_init()
{
	DISPLAY_clear();
	DISPLAY_write_line("Lives:", 7, 0, 0);
	DISPLAY_write_line("Score:", 7, 15, 0);
	DISPLAY_display[7][7] = '3';
	DISPLAY_display[7][22] = '0';
}

void DISPLAY_playboard_update(uint8_t lives, uint8_t score)
{
	DISPLAY_int(lives, 7, 7);
	DISPLAY_int(score, 7, 22);
}

